﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPY.LeHealth.DAL
{
    using ServiceStack.OrmLite;
    using System.Linq.Expressions;

    /// <summary>
    /// 数据操作基类（基于泛型的常用数据操作）
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    public class GenericBaseDAL<T>:BaseDAL
    {
        #region 通用查询

        #region 操作：插入

        /// <summary>
        /// 批量插入对象
        /// </summary>
        /// <param name="objs">对象列表</param>
        public void Insert(List<T> objs)
        {
            using (var cnn=DbConnection)
            {
                cnn.InsertAll<T>(objs);
            }
        }

        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>Id</returns>
        public long Insert(T obj)
        {
            using (var cnn=DbConnection)
            {
                var id = cnn.Insert<T>(obj, selectIdentity: true);
                return id;
            }
        }

        /// <summary>
        /// 插入数据（通用方法）
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="obj">实例</param>
        /// <param name="index">最后插入ID</param>
        /// <returns></returns>
        public bool Insert(T obj, out long index)
        {
            using (var cnn=DbConnection)
            {
                var id = cnn.Insert<T>(obj, selectIdentity: true);
                index = id;
                return id > 0;
            }
        }

        #endregion

        public bool Update(T obj)
        {
            using (var cnn=DbConnection)
            {
                var result = cnn.Update<T>(obj);
                return result > 0;
            }
        }

        public bool UpdateOnly(T obj,Expression<Func<T, object>> onlyFields)
        {
            using (var cnn=DbConnection)
            {
                var result= cnn.UpdateOnly(obj, onlyFields);
                return result > 0;
            }
        }

        public List<T> Select()
        {
            using (var cnn=DbConnection)
            {
               return cnn.Select<T>();
            }
        }

        /// <summary>
        /// 通用分页查询
        /// </summary>
        /// <param name="pageIndex">页序号（从1开始）</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        public List<T> SelectByPager(int pageIndex, int pageSize, out long totalSize)
        {
            using (var conn = DbConnection)
            {
                var ev = OrmLiteConfig.DialectProvider.SqlExpression<T>();
                ev.Limit((pageIndex - 1) * pageSize, pageSize);
                totalSize = conn.Count<T>();
                return conn.Select<T>(ev);
            }
        }

        public List<T> SelectByIds(System.Collections.IEnumerable ids)
        {
            using (var cnn = DbConnection)
            {
                return cnn.SelectByIds<T>(ids);
            }
        }

        public T SelectById(int id)
        {
            using (var cnn = DbConnection)
            {
                var rtv = cnn.SingleById<T>(id);
                return rtv;
            }
        }

        public bool DeleteById(int id)
        {
            using (var cnn=DbConnection)
            {
               var result= cnn.DeleteById<T>(id);
               return result > 0;
            }
        }

        #endregion
    }
}
